//
//  HGCircleCell.m
//  RedWinMall
//
//  Created by HeDianZhao on 2018/7/31.
//  Copyright © 2018年 HeDianZhao. All rights reserved.
//

#import "HGCircleCell.h"
#import "HGCircleModel.h"

@implementation HGCircleCell
{
    UILabel *_titleLabel;
    UILabel *_contentLabel;
    UILabel *_authorLabel;
    UILabel *_numLabel;
    UIImageView *_img;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self initSubViews];
    }
    return self;
}

- (void)setModel:(HGCircleModel *)model {
    
    _model = model;
    _titleLabel.attributedText=[self changeLineSpaceForStr:model.title];
    _authorLabel.text=[NSString stringWithFormat:@"%@ %@",model.cat_name,model.update_time];
    _numLabel.text=[NSString stringWithFormat:@"阅读 %ld",model.click];
    [_img sd_setImageWithURL:kPhotoURL(model.thumb) placeholderImage:nil];
    _contentLabel.attributedText = [self changeLineSpaceForStr:model.descriptions];
}

#pragma mark - Handle
-(NSAttributedString *)changeLineSpaceForStr:(NSString *)str
{
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, str.length)];
    return attributedString;
}

#pragma mark - Init
-(void)initSubViews
{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    CGFloat margin = 15;
    CGFloat font = 14*kScale;
    UIColor *textColor=kColorWithRGB(122, 122, 122);
    _img=[[UIImageView alloc]init];
    _img.contentMode=UIViewContentModeScaleAspectFit;
    [self addSubview:_img];
    [_img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).with.offset(-margin);
        make.top.equalTo(self).with.offset(margin);
        make.size.mas_equalTo(CGSizeMake(110 *kScale, 82 * kScale));
    }];


    _authorLabel=[[UILabel alloc]init];
    _authorLabel.text=@"巧巧 2018-01-01";
    _authorLabel.font=[UIFont systemFontOfSize:font-1];
    _authorLabel.textColor=textColor;
    [self addSubview:_authorLabel];
    [_authorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(margin);
        make.bottom.equalTo(_img.mas_bottom).with.offset(-5);
    }];
    _numLabel=[[UILabel alloc]init];
    _numLabel.text=@"阅读 0";
    _numLabel.textAlignment=NSTextAlignmentRight;
    _numLabel.textColor=textColor;
    _numLabel.font=[UIFont systemFontOfSize:font-1];
    [self addSubview:_numLabel];
    [_numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_img.mas_left).with.offset(-margin);
        make.centerY.mas_equalTo(_authorLabel.mas_centerY);
        make.left.equalTo(_authorLabel.mas_right).with.offset(margin);
    }];

    _titleLabel=[[UILabel alloc]init];
    _titleLabel.numberOfLines=2;
    _titleLabel.font=[UIFont systemFontOfSize:font+2];
    [self addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_img.mas_top).with.offset(5);
        make.left.mas_equalTo(_authorLabel.mas_left);
        make.right.mas_equalTo(_numLabel.mas_right);
    }];

    _contentLabel=[[UILabel alloc]init];
    _contentLabel.font=[UIFont systemFontOfSize:font];
    _contentLabel.numberOfLines=3;
    _contentLabel.textColor=textColor;
    [self addSubview:_contentLabel];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_img.mas_bottom).with.offset(10);
        make.left.mas_equalTo(_titleLabel.mas_left);
        make.right.mas_equalTo(_img.mas_right);
        make.bottom.equalTo(self).with.offset(-10);
    }];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


@end
